package cn.tianjindong.blog_manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tianjindong.blog.common.pojo.PageView;
import cn.tianjindong.blog.mapper.BlogUserMapper;
import cn.tianjindong.blog.mapper.BlogVisitlogMapper;
import cn.tianjindong.blog.pojo.BlogUser;
import cn.tianjindong.blog.pojo.BlogUserExample;
import cn.tianjindong.blog.pojo.BlogVisitlog;
import cn.tianjindong.blog.pojo.BlogVisitlogExample;
import cn.tianjindong.blog_manager.service.UserService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private BlogVisitlogMapper visitLogMapper;
	@Autowired
	private BlogUserMapper userMapper;

	@Override
	public PageView<BlogVisitlog> getUserVisitLogPageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogVisitlog> list = visitLogMapper.getVisitLogPageView(startIndex, countItem);

		// 获取数据库总条数
		int totalItem = visitLogMapper.countByExample(new BlogVisitlogExample());

		PageView<BlogVisitlog> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);

		return pageView;
	}

	@Override
	public PageView<BlogUser> getUserPageView(Integer currentPage, Integer countItem) {
		// 查询时开始索引
		int startIndex = (currentPage - 1) * countItem;
		List<BlogUser> list = userMapper.getUserPageView(startIndex, countItem);

		// 获取数据库总条数
		int totalItem = userMapper.countByExample(new BlogUserExample());

		PageView<BlogUser> pageView = new PageView<>();
		pageView.setCurrentPage(currentPage);
		pageView.setCountItem(countItem);
		pageView.setItems(list);
		pageView.setTotalItem(totalItem);
		return pageView;
	}

	@Override
	public void reverseUserStatus(Long userId) {
		BlogUser user = userMapper.selectByPrimaryKey(userId);
		if (user != null) {
			BlogUser tempUser = new BlogUser();
			tempUser.setId(userId);
			if(user.getActivation()==1){//处于激活状态
				tempUser.setActivation(-1);
				userMapper.updateByPrimaryKeySelective(tempUser);
			}else if(user.getActivation()==-1){//处于作废状态
				tempUser.setActivation(1);
				userMapper.updateByPrimaryKeySelective(tempUser);
			}
		}
	}
}
